using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using umbraco.cms.businesslogic.media;
using Commerce.Promotions;
using Commerce.Common;


namespace CommerceForUmbraco.usercontrols
{
    public partial class singleProduct : System.Web.UI.UserControl
    {
        #region Properties

        public string ProductName;
        public string SKU;
        public string ProductID;
        public string ShortDescription;
        public string Features;
        public decimal OurPrice;
        public decimal RetailPrice;
        public string ImageFile = "";
        public string ShippingEstimate;
        public string Rating;

        protected int productID;
        protected string productSku;
        protected Guid productGUID;

        private Commerce.Common.Product product = null;
        protected ProductDiscount discount;

        private string _PropProductId;

        public string PropProductId 
        {
            get { return _PropProductId; }
            set { _PropProductId = value; }
        }

        private bool _DontRedirectToCart;

        public bool DontRedirectToCart
        {
            get
            {
                return _DontRedirectToCart;
            }
            set
            {
                _DontRedirectToCart = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string sProductGUID = "";

                if (IsGUID(PropProductId))
                    sProductGUID = PropProductId;

                if (int.TryParse(PropProductId, out productID) == false)
                    productID = 0;

                productSku = PropProductId;

                if (sProductGUID != string.Empty)
                {
                    productGUID = new Guid(sProductGUID);
                    product = ProductController.GetProductDeepByGUID(productGUID);
                }
                else if (productID != 0)
                {
                    product = ProductController.GetProductDeep(productID);
                }
                else if (productSku != string.Empty)
                {
                    product = ProductController.GetProductDeep(productSku);
                }

                //make sure we have a product
                TestCondition.IsTrue(product.IsLoaded, "Invalid url/product id");

                //set the page variables
                productID = product.ProductID;
                productSku = product.Sku;
                ProductName = product.ProductName;
                RetailPrice = product.RetailPrice;
                // get the image URL from the Umbraco Node id
                Media m = new Media(int.Parse(product.DefaultImage));
                ImageFile = m.getProperty("umbracoFile").Value.ToString();
                ShippingEstimate = product.ShippingEstimate;
                OurPrice = product.OurPrice;
                ShortDescription = product.ShortDescription;

                // set the productID for cross sells
                CrossSellDisplay.relatedProductID = productID.ToString();

            }
            catch
            {

            }

        }

        protected decimal GetDiscountedPrice()
        {
            decimal dOut = OurPrice;

            //get the adjusted price from the Promo service
            ProductDiscount discount = PromotionService.GetProductDiscount(productID);

            if (discount != null)
                dOut = discount.DiscountedPrice;

            return dOut;

        }

        protected void AddToCart_Click(object sender, EventArgs e)
        {
            Commerce.Common.Product prod = new Commerce.Common.Product(productID);
            prod.Quantity = int.Parse(qtyToAdd.Text);
            OrderController.AddItem(prod);

            if (DontRedirectToCart)
            {
                CartEvent.Add(productID, int.Parse(qtyToAdd.Text));

            }
            else
            {
                // send to cart page
                Response.Redirect(Utility.GetCartUrl(), true);
            }

        }

        public static bool IsGUID(string expression)
        {
            if (expression != null)
            {
                Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

                return guidRegEx.IsMatch(expression);
            }
            return false;
        }
    }
}